﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class DrainageStateForm : Form
    {
        public DrainageStateForm()
        {
            InitializeComponent();
        }

        int avpID, regID, drID = 0, year;

        tb_drainageState drState;

        private void DrainageStateForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYearDr.Items.Add(i);
            }
            cbYearDr.SelectedIndex = cbYearDr.FindStringExact(DateTime.Now.Year.ToString());
            year = Convert.ToInt32(cbYearDr.Text);
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);

                cbDrainage.SelectedIndexChanged -= cbDrainage_SelectedIndexChanged;
                cbDrainage.DataSource = DBClass.Drainages
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbDrainage.DisplayMember = "Name";
                cbDrainage.ValueMember = "ID";
                cbDrainage.SelectedIndex = -1;
                cbDrainage.Text = string.Empty;
                cbDrainage.SelectedIndexChanged += new EventHandler(cbDrainage_SelectedIndexChanged);
            }
        }

        private void cbDrainage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbDrainage.SelectedIndex >= 0)
            {
                drID = Convert.ToInt32(cbDrainage.SelectedValue);
                ShowDrainageState();
            }
        }

        void ShowDrainageState()
        {

            if (drID > 0 && year > 0)
            {
                drState = DBClass.DrainagesState
                    .Where(wh => wh.DrainageID == drID && wh.Year == year)
                    .FirstOrDefault();
                if (drState != null)
                {
                    clearKDSTextBox.Text = drState.ClearKDS.ToString();
                    renovationKDSTextBox.Text = drState.RenovationKDS.ToString();
                    currentRepairTextBox.Text = drState.CurrentRepair.ToString();
                    capitalRepairTextBox.Text = drState.CapitalRepair.ToString();
                    meliorativeQuantityTextBox.Text = drState.MeliorativeQuantity.ToString();
                    irrigationQuantityTextBox.Text = drState.IrrigationQuantity.ToString();
                    badLengthTextBox.Text = drState.BadLength.ToString();
                    chbBalanceAVP.Checked = drState.BalanceAVP;
                }
                else
                {
                    clearKDSTextBox.Text = renovationKDSTextBox.Text = currentRepairTextBox.Text = capitalRepairTextBox.Text =
                        meliorativeQuantityTextBox.Text = irrigationQuantityTextBox.Text = badLengthTextBox.Text = string.Empty;
                    chbBalanceAVP.Checked = false;
                }
                
            }

        }

        private void cbYearDr_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYearDr.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYearDr.Text);
                ShowDrainageState();
            }
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            if (drID > 0 && year > 0)
            {
                decimal badLength, clKDS, RenKDS, curRepair, capRepair;
                int melQuantity, irrQuantity;

                bool correct = badLengthTextBox.Text.GetDecimalOrDefault(out badLength);
                correct &= clearKDSTextBox.Text.GetDecimalOrDefault(out clKDS);
                correct &= renovationKDSTextBox.Text.GetDecimalOrDefault(out RenKDS);
                correct &= currentRepairTextBox.Text.GetDecimalOrDefault(out curRepair);
                correct &= capitalRepairTextBox.Text.GetDecimalOrDefault(out capRepair);
                correct &= Int32.TryParse(meliorativeQuantityTextBox.Text, out melQuantity);
                correct &= Int32.TryParse(irrigationQuantityTextBox.Text, out irrQuantity);

                if (!correct)
                    if (MessageBox.Show(msgClass.msgUncorrectData_Continue, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                if (drState != null)
                {
                    drState.BadLength = badLength;
                    drState.BalanceAVP = chbBalanceAVP.Checked;
                    drState.CapitalRepair = capRepair;
                    drState.ClearKDS = clKDS;
                    drState.CurrentRepair = curRepair;
                    drState.IrrigationQuantity = irrQuantity;
                    drState.MeliorativeQuantity = melQuantity;
                    drState.RenovationKDS = RenKDS;
                }
                else
                {
                    drState = new tb_drainageState
                    {
                        BadLength = badLength,
                        BalanceAVP = chbBalanceAVP.Checked,
                        CapitalRepair = capRepair,
                        ClearKDS = clKDS,
                        CurrentRepair = curRepair,
                        DrainageID = drID,
                        IrrigationQuantity = irrQuantity,
                        MeliorativeQuantity = melQuantity,
                        RenovationKDS = RenKDS,
                        Year = year
                    };

                    DBClass.DrainagesState.InsertOnSubmit(drState);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
